package com.zz.nondatain;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.util.Log;

import androidx.viewpager2.widget.ViewPager2;

import com.zz.nondatain.base.BaseActivity;
import com.zz.nondatain.databinding.ActivityMainBinding;
import com.zz.nondatain.utils.FaceTokenExample;
import com.zz.nondatain.utils.FaceTokenManager;
import com.zz.nondatain.view.adapter.ViewPagerAdapter;
import com.zz.nondatain.view.fragment.HomeFragment;
import com.zz.nondatain.view.fragment.ProfileFragment;
import com.zz.nondatain.view.fragment.SignInFragment;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MainActivity extends BaseActivity<ActivityMainBinding> {

    private ViewPagerAdapter viewPagerAdapter;
    private static final String TAG = "MainActivity";
    
    @Override
    protected void initView() {


        
        // 初始化ViewPager
        setupViewPager();
        
        // 设置底部导航栏
        setupBottomNavigation();
    }



    private void setupViewPager() {
        viewPagerAdapter = new ViewPagerAdapter(this);
        viewPagerAdapter.addFragment(HomeFragment.newInstance());
        viewPagerAdapter.addFragment(SignInFragment.newInstance());
        viewPagerAdapter.addFragment(ProfileFragment.newInstance());
        
        binding.viewPager.setAdapter(viewPagerAdapter);
        
        // 设置ViewPager页面切换监听
        binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                // 根据页面位置选中对应的底部导航项
                switch (position) {
                    case 0:
                        binding.bottomNavigation.setSelectedItemId(R.id.navigation_home);
                        break;
                    case 1:
                        binding.bottomNavigation.setSelectedItemId(R.id.navigation_sign_in);
                        break;
                    case 2:
                        binding.bottomNavigation.setSelectedItemId(R.id.navigation_profile);
                        break;
                }
            }
        });
    }
    
    private void setupBottomNavigation() {
        binding.bottomNavigation.setOnItemSelectedListener(item -> {
            int id = item.getItemId();
            if (id == R.id.navigation_home) {
                binding.viewPager.setCurrentItem(0, true);
                return true;
            } else if (id == R.id.navigation_sign_in) {
                binding.viewPager.setCurrentItem(1, true);
                return true;
            } else if (id == R.id.navigation_profile) {
                binding.viewPager.setCurrentItem(2, true);
                return true;
            }
            return false;
        });
    }

    @Override
    protected void initData() {
        // 初始化数据
    }

    @Override
    protected void initObservers() {
        // 初始化观察者
    }

    @Override
    protected ActivityMainBinding getViewBinding() {
        return ActivityMainBinding.inflate(getLayoutInflater());
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 释放FaceTokenManager资源
        FaceTokenExample.disposeTokenManager(this);
    }

    /**
     * 导航到指定页面
     * @param pageIndex 页面索引（0：首页，1：签到页，2：个人中心）
     */
    public void navigateToPage(int pageIndex) {
        if (pageIndex >= 0 && pageIndex < viewPagerAdapter.getItemCount()) {
            binding.viewPager.setCurrentItem(pageIndex, true);
        }
    }
}